package com.redlight.jdbc;

import com.redlight.base.BaseBean;
import com.redlight.utils.ApplicationContextProvider;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author chao
 * @Title: JdbcUtil
 * @date 2019-01-24 21:42
 */
public class JdbcUtil {
    private static DataSource dataSource;

    public JdbcUtil() {
    }

    public static boolean execute(String sql, JdbcParam jdbcParam) throws Exception {
        System.out.println("===> " + sql);
        Connection conn = getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        if (jdbcParam != null) {
            int i = 0;
            Iterator var5 = jdbcParam.iterator();

            while(var5.hasNext()) {
                Object paramO = var5.next();
                JdbcParam.ParamData pd = (JdbcParam.ParamData)paramO;
                Object value = pd.getValue();
                Class type = pd.getType();
                System.out.print("  [" + value + ":" + type.getName() + "]");
                if (type.equals(String.class)) {
                    ++i;
                    pstmt.setString(i, value.toString());
                } else if (type.equals(Integer.class)) {
                    ++i;
                    pstmt.setInt(i, Integer.parseInt(value.toString()));
                }
            }
        }

        pstmt.executeUpdate();
        close(pstmt, conn);
        return true;
    }

    public static List queryMapList(String sql, JdbcParam jdbcParam) throws Exception {
        System.out.println("===> " + sql);
        Connection conn = getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        if (jdbcParam != null) {
            int i = 0;
            Iterator var5 = jdbcParam.iterator();

            while(var5.hasNext()) {
                Object paramO = var5.next();
                JdbcParam.ParamData pd = (JdbcParam.ParamData)paramO;
                Object value = pd.getValue();
                Class type = pd.getType();
                System.out.print("  [" + value + ":" + type.getName() + "]");
                if (type.equals(String.class)) {
                    ++i;
                    pstmt.setString(i, value.toString());
                } else if (type.equals(Integer.class)) {
                    ++i;
                    pstmt.setInt(i, Integer.parseInt(value.toString()));
                }
            }
        }

        ResultSet rs = pstmt.executeQuery();
        List list = resultSetToMap(rs);
        close(pstmt, rs, conn);
        return list;
    }

    public static List queryList(String sql, JdbcParam jdbcParam, Class<? extends BaseBean> c) throws Exception {
        System.out.print("===> " + sql);
        Connection conn = getConnection();
            PreparedStatement pstmt = conn.prepareStatement(sql);
        if (jdbcParam != null) {
            int i = 0;
            Iterator var6 = jdbcParam.iterator();

            while(var6.hasNext()) {
                Object paramO = var6.next();
                JdbcParam.ParamData pd = (JdbcParam.ParamData)paramO;
                Object value = pd.getValue();
                Class type = pd.getType();
                System.out.print("  [" + value + ":" + type.getName() + "]");
                if (type.equals(String.class)) {
                    ++i;
                    pstmt.setString(i, value.toString());
                } else if (type.equals(Integer.class)) {
                    ++i;
                    pstmt.setInt(i, Integer.parseInt(value.toString()));
                }
            }
        }

        ResultSet rs = pstmt.executeQuery();
        List list = resultSetToObject(rs, c);
        close(pstmt, rs, conn);
        return list;
    }

    public static List resultSetToObject(ResultSet rs, Class outputClass) {
        ArrayList outputList = new ArrayList();

        try {
            if (rs != null) {
                ResultSetMetaData rsmd = rs.getMetaData();
                Field[] declearFields = outputClass.getDeclaredFields();
                Field[] superFields = outputClass.getSuperclass().getDeclaredFields();
                Field[] fields = new Field[declearFields.length + superFields.length];
                int i = 0;
                Field[] var8 = declearFields;
                int _iterator = declearFields.length;

                int var10;
                Field f1;
                for(var10 = 0; var10 < _iterator; ++var10) {
                    f1 = var8[var10];
                    fields[i++] = f1;
                }

                var8 = superFields;
                _iterator = superFields.length;

                for(var10 = 0; var10 < _iterator; ++var10) {
                    f1 = var8[var10];
                    fields[i++] = f1;
                }

                while(rs.next()) {
                    Object bean = outputClass.newInstance();

                    for(_iterator = 0; _iterator < rsmd.getColumnCount(); ++_iterator) {
                        String columnName = rsmd.getColumnName(_iterator + 1);
                        Object columnValue = rs.getObject(_iterator + 1);
                        String fieldName = null;
                        Field[] var13 = fields;
                        int var14 = fields.length;

                        for(int var15 = 0; var15 < var14; ++var15) {
                            Field field = var13[var15];
                            if (field.isAnnotationPresent(Column.class)) {
                                Column column = (Column)field.getAnnotation(Column.class);
                                String columnVal = StringUtils.isNotBlank(column.value()) ? column.value() : field.getName();
                                if (columnVal.equalsIgnoreCase(columnName) && columnValue != null) {
                                    BeanUtils.setProperty(bean, field.getName(), columnValue);
                                    break;
                                }
                            }
                        }
                    }

                    outputList.add(bean);
                }
            }
        } catch (IllegalAccessException var19) {
            var19.printStackTrace();
        } catch (SQLException var20) {
            var20.printStackTrace();
        } catch (InstantiationException var21) {
            var21.printStackTrace();
        } catch (InvocationTargetException var22) {
            var22.printStackTrace();
        }

        return outputList;
    }

    public static List<LinkedHashMap> resultSetToMap(ResultSet rs) {
        ArrayList outputList = new ArrayList();

        try {
            if (rs != null) {
                ResultSetMetaData rsmd = rs.getMetaData();

                while(rs.next()) {
                    LinkedHashMap map = new LinkedHashMap();

                    for(int _iterator = 0; _iterator < rsmd.getColumnCount(); ++_iterator) {
                        String columnName = rsmd.getColumnLabel(_iterator + 1);
                        Object columnValue = rs.getObject(_iterator + 1);
                        map.put(columnName, columnValue);
                    }

                    outputList.add(map);
                }
            }
        } catch (SQLException var7) {
            var7.printStackTrace();
        }

        return outputList;
    }

    public static Connection getConnection() throws Exception {
        Connection conn = null;

        try {
            if (dataSource == null) {
//                ApplicationContextProvider.getBean()
                JdbcTemplate jdbcTemplate = (JdbcTemplate) ApplicationContextProvider.getBean(JdbcTemplate.class);
                dataSource = jdbcTemplate.getDataSource();
            }

            conn = dataSource.getConnection();
            return conn;
        } catch (Exception var2) {
            var2.printStackTrace();
            throw var2;
        }
    }

    public static void close(Object... objs) {
        if (objs != null) {
            Object[] var1 = objs;
            int var2 = objs.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                Object o = var1[var3];
                if (o instanceof ResultSet) {
                    try {
                        ((ResultSet)o).close();
                    } catch (SQLException var8) {
                        var8.printStackTrace();
                    }
                } else if (o instanceof Statement) {
                    try {
                        ((Statement)o).close();
                    } catch (SQLException var7) {
                        var7.printStackTrace();
                    }
                } else if (o instanceof Connection) {
                    try {
                        ((Connection)o).close();
                    } catch (SQLException var6) {
                        var6.printStackTrace();
                    }
                }
            }
        }

    }

    public static void main(String[] args) {
        System.out.println(String.class.equals(String.class));
    }
}
